package com.linfnn.demo.Controller;

import com.linfnn.demo.Model.Healthplan;
import com.linfnn.demo.Service.impl.HealthplanServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import java.util.List;

@Controller
public class FunctionHealthplan {
    @Autowired
    private HealthplanServiceImpl healthplanService;

    //  这是管理页面，同时可以刷新数据，进行操作后跳到这里会重新获取数据，从而刷新页面
    @RequestMapping(value = "/manage_healthplans")
    public ModelAndView showAll() {
        List<Healthplan> healthplans = healthplanService.list();
        //        返回的页面
        ModelAndView mav = new ModelAndView("/managehtml/healthplanManage.html");
        //        页面和对应的数据结合
        mav.addObject("results", healthplans);
        return mav;
    }

    //
    @PostMapping("/search/Healthplan")
    public ModelAndView search(@RequestParam("PlanCode") String PlanCode) {
        Healthplan healthplan = healthplanService.getHealthplan(PlanCode);
        ModelAndView mav = new ModelAndView("/search/index2.html");
        mav.addObject("results", healthplan);
        return mav;
    }


    //    注意这里返回应该更换为html，不能再去直接刷新获取整个数据库的数据， 并且这里的路由为searchadmin，前面有一个search，两者不一样
    @PostMapping("/searchadmin/Healthplan")
    public ModelAndView searchadmin(@RequestParam("PlanCode") String PlanCode) {
        Healthplan healthplan = healthplanService.getHealthplan(PlanCode);
        ModelAndView mav = new ModelAndView("/managehtml/healthplanManage.html");
        mav.addObject("results", healthplan);
        return mav;
    }


    @RequestMapping("/delete/Healthplan")
    public ModelAndView deleteHealthplan(@RequestParam("PlanCode") String PlanCode) {
        // Delete assessment logic here
        if (PlanCode.isEmpty()){
            System.out.println("delete输入为空");
        }
        if(healthplanService.removeById(PlanCode)){
            System.out.println("删除成功");
        };

        ModelAndView mav = new ModelAndView("redirect:/manage_healthplans");
        return mav;
    }


    //    这里点击更新后，会跳转到xxxUpdata.html文件里面去，然后再执行更新
    @RequestMapping("/updatehtml/Healthplan")
    public ModelAndView updateHealthplan() {
        return new ModelAndView("redirect:/updatahtml/healthplanUpdata.html");
    }

    @RequestMapping("/update/Healthplan")
    public ModelAndView updateHealthplan(@RequestParam("PlanCode") String PlanCode,
                                         @RequestParam("PlanName") String PlanName,
                                         @RequestParam("TargetGroup") String TargetGroup,
                                         @RequestParam("StartTime") String StartTime,
                                         @RequestParam("EndTime") String EndTime,
                                         @RequestParam("MainContent") String MainContent,
                                         @RequestParam("ExpectedEffect") String ExpectedEffect,
                                         @RequestParam("Status") String Status,
                                         @RequestParam("Remarks") String Remarks
                                         ) {
        // Update assessment logic here
//    这里逻辑比较复杂，为了方便，这里使用的是先删除对应数据，然后插入该数据的操作
        if(healthplanService.removeById(PlanCode)){
            System.out.println("更新——删除成功");
        };

       Healthplan healthplan = new Healthplan();
        healthplan.setPlanCode(PlanCode);
        healthplan.setPlanName(PlanName);
        healthplan.setTargetGroup(TargetGroup);
        healthplan.setStartTime(StartTime);
        healthplan.setEndTime(EndTime);
        healthplan.setMainContent(MainContent);
        healthplan.setExpectedEffect(ExpectedEffect);
        healthplan.setStatus(Status);
        healthplan.setRemarks(Remarks);



        if(healthplanService.save(healthplan)){
            System.out.println("更新——添加成功");
        };

        return new ModelAndView("redirect:/manage_healthplans");
    }



    @RequestMapping("/add/Healthplan")
    public ModelAndView addHealthplan(@RequestParam("PlanCode") String PlanCode,
                                      @RequestParam("PlanName") String PlanName,
                                      @RequestParam("TargetGroup") String TargetGroup,
                                      @RequestParam("StartTime") String StartTime,
                                      @RequestParam("EndTime") String EndTime,
                                      @RequestParam("MainContent") String MainContent,
                                      @RequestParam("ExpectedEffect") String ExpectedEffect,
                                      @RequestParam("Status") String Status,
                                      @RequestParam("Remarks") String Remarks
    ) {
        // Add assessment logic here
        Healthplan healthplan = new Healthplan();
        healthplan.setPlanCode(PlanCode);
        healthplan.setPlanName(PlanName);
        healthplan.setTargetGroup(TargetGroup);
        healthplan.setStartTime(StartTime);
        healthplan.setEndTime(EndTime);
        healthplan.setMainContent(MainContent);
        healthplan.setExpectedEffect(ExpectedEffect);
        healthplan.setStatus(Status);
        healthplan.setRemarks(Remarks);


        if(healthplanService.save(healthplan)){
            System.out.println("添加成功");
        };
        return new ModelAndView("redirect:/manage_healthplans");
    }

}
